package io.geekidea.springbootplus.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.core.pagination.PageInfo;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.util.PhoneUtil;
import io.geekidea.springbootplus.system.entity.SysSubscribe;
import io.geekidea.springbootplus.system.entity.SysUser;
import io.geekidea.springbootplus.system.mapper.SysSubscribeMapper;
import io.geekidea.springbootplus.system.param.SysSubscribePageParam;
import io.geekidea.springbootplus.system.service.SysSubscribeService;
import io.geekidea.springbootplus.system.vo.SysSubscribeQueryVo;
import io.geekidea.springbootplus.system.vo.SysUserQueryVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class SysSubscribeServiceImpl extends BaseServiceImpl<SysSubscribeMapper, SysSubscribe> implements SysSubscribeService {

    @Resource
    private SysSubscribeMapper sysSubscribeMapper;

    @Override
    public boolean addToSubscribe(SysSubscribe sysSubscribe) throws Exception {
        return sysSubscribeMapper.insertSubscribe(sysSubscribe)>0;
    }

    @Override
    public boolean deleteFromSubscribe(int id) throws Exception {
        return sysSubscribeMapper.deleteSubscribe(id)>0;
    }

    @Override
    public boolean deleteAllFromSubscribe(List<Integer> ids) throws Exception {
        return sysSubscribeMapper.deleteAllFromSubscribe(ids)>0;
    }

    @Override
    public boolean updateSubscribeById(SysSubscribe sysSubscribe) throws Exception {
        return sysSubscribeMapper.updateSubscribeById(sysSubscribe)>0;
    }

    @Override
    public Paging<SysSubscribeQueryVo> findSysSubscribeByPhone(SysSubscribePageParam subscribe) throws Exception {
        Page<SysSubscribeQueryVo> page = new PageInfo<>(subscribe, OrderItem.desc(getLambdaColumn(SysSubscribe::getId)));
        IPage<SysSubscribeQueryVo> iPage = sysSubscribeMapper.getSysSubscribePageList(page, subscribe);

        // 手机号码脱敏处理
        if (iPage != null && CollectionUtils.isNotEmpty(iPage.getRecords())) {
            iPage.getRecords().forEach(vo -> {
                vo.setPhone(PhoneUtil.desensitize(vo.getPhone()));
            });
        }
        return new Paging(iPage);
    }

    @Override
    public SysSubscribeQueryVo getSubscribe(Serializable id) throws Exception {
        return sysSubscribeMapper.getSubscribe(id);
    }


}
